
/*FILE UPDATE INFO**************************************************************
 * Author   : PURESUN
 * Update   : 2023-12-27
 * Info     : coating process component
 * Version  : 1.0.0
 ******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __COATING_PRO_H__
#define __COATING_PRO_H__

/* Includes ------------------------------------------------------------------*/
#include "stdbool.h"
#include "puresun_type.h"
#ifndef __COATING_PRO_H__
#include "coating_pro_conf_template.h"
#endif

/* Exported macro ------------------------------------------------------------*/
#ifndef MAX_PRO_NAME_LEN
#define MAX_PRO_NAME_LEN 64
#endif // MAX_PRO_NAME_LEN

#ifndef MAX_PRO_DESCRIPTION_LEN
#define MAX_PRO_DESCRIPTION_LEN 256
#endif // MAX_PRO_DESCRIPTION_LEN

#ifndef MAX_STEP_NOTES_LEN
#define MAX_STEP_NOTES_LEN 256
#endif // MAX_STEP_NOTES_LEN

#ifndef MAX_STEPS_COUNT
#define MAX_STEPS_COUNT 32
#endif // MAX_STEPS_COUNT

#ifndef MAX_PRO_COUNT
#define MAX_PRO_COUNT 16
#endif // MAX_PRO_COUNT

#ifndef COATING_PRO_JSON_PATH_MAX_LEN
#define COATING_PRO_JSON_PATH_MAX_LEN 256
#endif // COATING_PRO_JSON_PATH_MAX_LEN

#ifndef DEFAULT_COPRO_ID
// default coating process id, if not found in json file or not a number, the first coating process id is 1
#define DEFAULT_COPRO_ID 1
#endif // DEFAULT_COPRO_ID

#ifndef DEFAULT_COPRO_NAME
#define DEFAULT_COPRO_NAME "not set"
#endif // DEFAULT_COPRO_NAME

#ifndef DEFAULT_COPRO_DESCRIPTION
#define DEFAULT_COPRO_DESCRIPTION "not set"
#endif // DEFAULT_COPRO_DESCRIPTION

#ifndef DEFAULT_COPRO_STEPS_COUNT
#define DEFAULT_COPRO_STEPS_COUNT 0
#endif // DEFAULT_COPRO_STEPS_COUNT

#ifndef DEFAULT_STEP_ID
// default step id, if not found in json file or not a number, the first step id is 1
#define DEFAULT_STEP_ID 0
#endif // DEFAULT_STEP_ID

#ifndef DEFAULT_STEP_STATUS
#define DEFAULT_STEP_STATUS false
#endif // DEFAULT_STEP_STATUS

#ifndef DEFAULT_STEP_DURATION
#define DEFAULT_STEP_DURATION 0
#endif // DEFAULT_STEP_DURATION

#ifndef DEFAULT_STEP_COOLDOWN
#define DEFAULT_STEP_COOLDOWN 0
#endif // DEFAULT_STEP_COOLDOWN

#ifndef DEFAULT_STEP_CURRENT
#define DEFAULT_STEP_NOTES "not set"
#endif // DEFAULT_STEP_NOTES
#define DEFAULT_STEP_POWER 0

#ifndef DEFAULT_STEP_DURATION_MIN
// default step duration min value, unit is second, range is 0 ~ 3600 from 0s ~ 3600s
#define DEFAULT_STEP_DURATION_MIN 0
#endif // DEFAULT_STEP_DURATION_MIN

#ifndef DEFAULT_STEP_DURATION_MAX
// default step duration max value, unit is second, range is 0 ~ 3600 from 0s ~ 3600s
#define DEFAULT_STEP_DURATION_MAX 3600
#endif // DEFAULT_STEP_DURATION_MAX

#ifndef DEFAULT_STEP_COOLDOWN_MIN
// default step cooldown min value, unit is second, range is 0 ~ 3600 from 0s ~ 3600s
#define DEFAULT_STEP_COOLDOWN_MIN 0
#endif // DEFAULT_STEP_COOLDOWN_MIN

#ifndef DEFAULT_STEP_COOLDOWN_MAX
// default step cooldown max value, unit is second, range is 0 ~ 3600 from 0s ~ 3600s
#define DEFAULT_STEP_COOLDOWN_MAX 3600
#endif // DEFAULT_STEP_COOLDOWN_MAX

#ifndef DEFAULT_STEP_POWER_MIN
// default step current min value, unit is dA, range is 0 ~ 5000 from 0.00A ~ 50.00A
#define DEFAULT_STEP_POWER_MIN 0
#endif // DEFAULT_STEP_CURRENT_MIN

#ifndef DEFAULT_STEP_POWER_MAX
// default step current max value, unit is dA, range is 0 ~ 5000 from 0.00A ~ 50.00A
#define DEFAULT_STEP_POWER_MAX 99
#endif // DEFAULT_STEP_CURRENT_MAX

/* Exported typedef -----------------------------------------------------------*/

typedef enum
{
    COA_OK = 0,
    COA_ERR,
    COA_ERR_CP_IS_NULL,
    COA_ERR_CPL_IS_NULL,
    COA_ERR_CP_CNT_NOT_FOUND,
    COA_ERR_NO_MEM,
    COA_ERR_PID_NOT_FOUND,
    COA_ERR_PID_NOT_NUMBER,
    COA_ERR_PID_NOT_IN_RANGE,
    COA_ERR_CP_LIST_NOT_FOUND,
    COA_ERR_PNAME_NOT_FOUND,
    COA_ERR_PNAME_NOT_STRING,
    COA_ERR_PNAME_TOO_SHORT,
    COA_ERR_TOKENS_IS_NULL,
    COA_ERR_TOKEN_COUNT_ZERO,
    COA_ERR_TOKEN_COUNT_NEGATIVE,
    COA_ERR_TOKEN_COUNT_TOO_SHORT,
    COA_ERR_JSON_FILE_NOT_EXIST,
    COA_ERR_JSON_FILE_EXIST,
    COA_ERR_JSON_FILE_READ_FAILED,
    COA_ERR_JSON_FILE_WRITE_FAILED,
    COA_ERR_JSON_FILE_PARSE_ERROR,
    COA_ERR_JSON_FILE_PATH_IS_INVALID,
    COA_ERR_JSON_VESSLE_ERROR,
    COA_ERR_STEP_COUNT_NOT_FOUND,
    COA_ERR_STEP_COUNT_NOT_NUMBER,
    COA_ERR_STEPS_IS_NULL,
    COA_ERR_STEPS_COUNT_NOT_MATCH,
    COA_ERR_STEPS_IS_NOT_ARRAY,
    COA_ERR_STEPS_ARRAY_NOT_FOUND,
    COA_ERR_STEP_ID_NOT_FOUND,
    COA_ERR_STEP_ID_NOT_NUMBER,
    COA_ERR_STEP_STATUS_NOT_FOUND,
    COA_ERR_STEP_STATUS_NOT_BOOL,
    COA_ERR_STEP_DURATION_NOT_FOUND,
    COA_ERR_STEP_DURATION_NOT_NUMBER,
    COA_ERR_STEP_COOLDOWN_NOT_FOUND,
    COA_ERR_STEP_COOLDOWN_NOT_NUMBER,
    COA_ERR_STEP_POWER_NOT_IN_RANGE,
    COA_ERR_STEP_DURATION_NOT_IN_RANGE,
    COA_ERR_STEP_COOLDOWN_NOT_IN_RANGE,

} coa_err_t;

typedef struct _steps_
{
    bool status;          // true: enable, false: disable
    int id;               // step id
    string notes;         // step notes
    int duration;         // step duration
    int cooldown;         // step cooldown
    int power_set;          // current strength, unit is dA, range is 0 ~ 5000 from 0.00A ~ 50.00A
    struct _steps_ *next; // next step
} step_t;

typedef struct _coating_program_
{
    int id;
    string name;
    string description;
    int steps_count;
    step_t *steps;
    step_t *current_step;
    struct _coating_program_ *next;
    // string product_name;
} coating_program_t;

typedef struct _coating_program_list_
{
    int program_count;
    int default_program_id;
    string json_file_path;
    coating_program_t *program;
    coating_program_t *current_program;
} coating_program_list_t;

/* Exported types ------------------------------------------------------------*/

/* Exported constants --------------------------------------------------------*/

/* Exported variables --------------------------------------------------------*/

/* Exported functions --------------------------------------------------------*/
// debug function
void free_program_list(coating_program_list_t *cpl);

// user function
coa_err_t coating_program_list_init(coating_program_list_t *cpl, string json_file_path);
void print_coating_program_list(coating_program_list_t *cpl, bool print_steps, bool print_current_program);
coa_err_t save_coating_program_list(coating_program_list_t *cpl, string json_file_path, bool cover);
coa_err_t save_coating_program_list_no_space(coating_program_list_t *cpl, string json_file_path, bool cover);
coa_err_t new_coating_program(coating_program_list_t *cpl);
coa_err_t new_step_to_program_at_tail(coating_program_list_t *cpl, int program_id);
coa_err_t edit_program(coating_program_list_t *cpl, int pid, string name, string description);
coa_err_t edit_step(coating_program_list_t *cpl, int pid, int step_id, string notes, int duration, int cooldown, int current);
coa_err_t set_step_status(coating_program_list_t *cpl, int pid, int step_id, bool status);
coa_err_t set_current_program(coating_program_list_t *cpl, int pid);
coa_err_t set_current_step(coating_program_list_t *cpl, int pid, int step_id);
coa_err_t delete_program(coating_program_list_t *cpl, int pid);
coa_err_t delete_step(coating_program_list_t *cpl, int pid, int step_id);
coa_err_t move_step(coating_program_list_t *cpl, int pid, int step_id, bool forward);
coa_err_t copy_step(coating_program_list_t *cpl, int pid, int step_id);
coa_err_t copy_program(coating_program_list_t *cpl, int pid);
coating_program_t *get_program_by_id(coating_program_list_t *cpl, int pid);
int get_steps_count(coating_program_list_t *cpl, int pid);

#endif // __COATING_PRO_H__
/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
